Skip to content

chore(generator): delegate request-id setup to public request#17739

Open
hebaalazzeh wants to merge 38 commits into
mainfrom
feat/gapic-generator-centralization-request-id
Open

chore(generator): delegate request-id setup to public request#17739
hebaalazzeh wants to merge 38 commits into
mainfrom
feat/gapic-generator-centralization-request-id

Conversation

@hebaalazzeh

@hebaalazzeh hebaalazzeh commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Updates generator templates and goldens to import public requests from google-api-core gapic_v1 and call setup_request_id helper. Removes duplicate setup_request_id test logic from generated client unit tests.

Temporarily pointing to this PR then will revert once it is merged/released:

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the request ID setup logic in the generated client templates by delegating it to google.api_core.gapic_v1.method_helpers.setup_request_id instead of maintaining an inline implementation. As a result, the redundant unit tests for _setup_request_id have been removed from both the templates and the golden integration tests. There are no review comments, so I have no feedback to provide.

@hebaalazzeh
hebaalazzeh changed the base branch from main to feat/gapic-centralization-api-core-request-id July 16, 2026 15:20
@hebaalazzeh hebaalazzeh self-assigned this Jul 16, 2026
@hebaalazzeh hebaalazzeh reopened this Jul 16, 2026
@hebaalazzeh
hebaalazzeh force-pushed the feat/gapic-generator-centralization-request-id branch from f037044 to f25d768 Compare July 16, 2026 16:10
@hebaalazzeh
hebaalazzeh force-pushed the feat/gapic-centralization-api-core-request-id branch from a0bff77 to 613b392 Compare July 16, 2026 16:18
@hebaalazzeh
hebaalazzeh force-pushed the feat/gapic-generator-centralization-request-id branch from f25d768 to 71b2f4e Compare July 16, 2026 16:20
@hebaalazzeh
hebaalazzeh force-pushed the feat/gapic-centralization-api-core-request-id branch 3 times, most recently from e02ee4b to db040bb Compare July 16, 2026 16:28
@daniel-sanche daniel-sanche added the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Jul 16, 2026
@hebaalazzeh
hebaalazzeh force-pushed the feat/gapic-generator-centralization-request-id branch from 71b2f4e to a8cc8d8 Compare July 16, 2026 18:11
@daniel-sanche

Copy link
Copy Markdown
Contributor

This implementation makes sense if we plan to bump up the api_core requirement Before merging, we need to:

  1. get the changes in feat(api_core): add request-id auto-population logic to gapic_v1 public helpers #17738 released
  2. update the setup.py to reference the new api_core version floor
  3. regenerate goldens

There's also a chance we will want to keep a fallback implementation to keep supporting old api_core versions

@hebaalazzeh hebaalazzeh changed the title feat: delegate request-id setup to public method_helpers chore(generator): delegate request-id setup to public request Jul 16, 2026
…lic helpers

Introduces method_helpers module containing setup_request_id helper. Exposes the module as public in gapic_v1.
@hebaalazzeh
hebaalazzeh force-pushed the feat/gapic-centralization-api-core-request-id branch from 90602cc to 76ac456 Compare July 16, 2026 23:21
…lic helpers

Introduces method_helpers module containing setup_request_id helper. Exposes the module as public in gapic_v1.
Updates generator templates and goldens to import public method_helpers from google-api-core gapic_v1 and call setup_request_id helper. Removes duplicate setup_request_id test logic from generated client unit tests.
@hebaalazzeh
hebaalazzeh force-pushed the feat/gapic-centralization-api-core-request-id branch from 8e31d83 to 20adea6 Compare July 16, 2026 23:42
@hebaalazzeh
hebaalazzeh force-pushed the feat/gapic-generator-centralization-request-id branch 2 times, most recently from 20e4bea to a8105ff Compare July 21, 2026 20:23
setattr(request, field_name, request_id_val)
except (AttributeError, ValueError):
# Proto-plus messages or other objects
if not getattr(request, field_name, None):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line consistent to what we have in the requests.py in api-core?

request (Union[google.protobuf.message.Message, dict]): The request object.
field_name (str): The name of the field to populate.
is_proto3_optional (bool): Whether the field is proto3 optional.
"""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need the check below (similar to what we have in api-core)

if request is None:
    return

from google.api_core.client_options import ClientOptions
from google.api_core import exceptions as core_exceptions
from google.api_core import gapic_v1
{% if has_auto_populated_fields.value %}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we only generate this file when has_auto_populated_fields is true?

Comment on lines +20 to +22
except ImportError: # pragma: NO COVER
# TODO(https://github.com/googleapis/google-cloud-python/issues/17813): Remove this fallback when google-api-core >= 2.26.0 is the minimum required version.
def setup_request_id(request, field_name: str, is_proto3_optional: bool): # pragma: NO COVER

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need these pragma comments?

request (Union[google.protobuf.message.Message, dict]): The request object.
field_name (str): The name of the field to populate.
is_proto3_optional (bool): Whether the field is proto3 optional.
"""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the diff here looks pretty broken

# See the License for the specific language governing permissions and
# limitations under the License.
#

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should have a file-level docstring explaining this file

)

self._client._setup_request_id(request, 'request_id', False)
setup_request_id(request, 'request_id', False)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't this need to be imported?


try:
from google.api_core.gapic_v1.requests import setup_request_id # type: ignore
except ImportError: # pragma: NO COVER

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need the NO COVER here? It feels like it could be risky if this code isn't covered. We should have some tests that cover the lower bound for api-core, which would hit this

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a similar comment above i.e. we'll probably miss coverage and adding # pragma: NO COVER is not the right way to silence coverage here.

However, I believe _compat.py will only generate static / helper code. Ideally, all the code in here should be identical to what we add to api-core (we should have a strict contract about this somewhere in the file). Given that, generating these tests don't really provide any value (if these tests are already well tested in api-core).

So it shouldn't be risky.

@hebaalazzeh
hebaalazzeh force-pushed the feat/gapic-generator-centralization-request-id branch from 4659080 to e243bbd Compare July 21, 2026 20:38
if not opts.metadata and template_name.endswith("gapic_metadata.json.j2"):
return answer

# Only render _compat.py.j2 if the API schema has auto_populated_fields

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this a bit more? What are auto_populated_fields?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not merge Indicates a pull request not ready for merge, due to either quality or timing.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants